const electron = require('electron')
const path = require('path')
const { spawn } = require('child_process')
const chalk = require('chalk')
module.exports = () => {
  const electronProcess = spawn(electron, ['--inspect=5858', path.join(__dirname, '../electron-dist/index.js')])
  electronProcess.stdout.on('data', data => {
    electronLog(data, 'blue')
  })
  electronProcess.stderr.on('data', data => {
    electronLog(data, 'red')
  })
  electronProcess.stderr.on('close', data => {
    process.exit()
  })
}

function electronLog (data, color) {
  let log = ''
  data = data.toString().split(/\r?\n/)
  data.forEach(line => {
    log += `  ${line}\n`
  })
  if (/[0-9A-z]+/.test(log)) {
    console.log(
      chalk[color].bold('┏ Electron -------------------') +
      '\n\n' +
      log +
      chalk[color].bold('┗ ----------------------------') +
      '\n'
    )
  }
}
